/*
 * @file gpio_config.c
 * @brief This file contains functions for configuring GPIO settings on a specific chipset.
 * It provides functions to set the pull-up/pull-down configuration, set the function of a GPIO pin,
 * and to sleep for a specified period in milliseconds. The implementation varies depending on the chipset.
 * This code includes logic for different chips, specifically for the WS63 chip and others.
 * 
 * This code is open-sourced under the Apache 2.0 license. When using open-source libraries
 * or code, please ensure to acknowledge the original authors and sources.
 */

#include "iot_errno.h"
#include "iot_gpio_ex.h"
#if CHIP_WS63
#include "pinctrl.h"
#include "los_base.h"
#else
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_task.h"
#include "hi_types_base.h"
#endif

#if CHIP_WS63
/**
 * @brief Set the pull-up or pull-down configuration for a specific GPIO pin.
 * @param id GPIO pin identifier.
 * @param val The pull configuration to be applied (e.g., pull-up, pull-down, or none).
 * @return Returns IOT_FAILURE if id is invalid or an error occurs, otherwise returns the result of the configuration operation.
 */
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    // If id is greater than or equal to PIN_NONE, return IOT_FAILURE
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    // Call the uapi_pin_set_pull function to set the pull configuration
    return uapi_pin_set_pull((pin_t)id, (pin_pull_t)val);
}

/**
 * @brief Set the function (mode) of a specific GPIO pin.
 * @param id GPIO pin identifier.
 * @param val The function mode to be set.
 * @return Returns IOT_FAILURE if id is invalid, otherwise returns the result of the configuration operation.
 */
unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    return uapi_pin_set_mode((pin_t)id, val);
}

/**
 * @brief Sleep for a specified number of milliseconds.
 * @param ms The number of milliseconds to sleep.
 * @return Returns IOT_FAILURE if the sleep time is invalid, otherwise returns IOT_SUCCESS.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    
    // Call LOS_Msleep to put the task to sleep for the given time in milliseconds
    LOS_Msleep((unsigned int)ms);

    return IOT_SUCCESS;
}
#else
/**
 * @brief Set the pull-up or pull-down configuration for a specific GPIO pin.
 * @param id GPIO pin identifier.
 * @param val The pull configuration to be applied (e.g., pull-up, pull-down, or none).
 * @return Returns IOT_FAILURE if id is invalid or an error occurs, otherwise returns the result of the configuration operation.
 */
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_pull((hi_io_name)id, (hi_io_pull)val);
}

/**
 * @brief Set the function (mode) of a specific GPIO pin.
 * @param id GPIO pin identifier.
 * @param val The function mode to be set.
 * @return Returns IOT_FAILURE if id is invalid, otherwise returns the result of the configuration operation.
 */
unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_func((hi_io_name)id, val);
}

/**
 * @brief Sleep for a specified number of milliseconds.
 * @param ms The number of milliseconds to sleep.
 * @return Returns IOT_FAILURE if the sleep time is invalid, otherwise returns IOT_SUCCESS.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    // Call hi_sleep to sleep for the given number of milliseconds
    return hi_sleep((hi_u32)ms);
}
#endif
